#include <memory.h>
#include "CPacket.h"

CPacket::CPacket()
{
	memset(m_szBuf, 0, sizeof(char) * MAX_BUFFER);
	m_nLen				= 0;
	m_nPos				= 0;
	m_nExpecting		= 0;
	m_nGotten			= 0;
	m_nSent				= 0;
}

unsigned __int16 CPacket::InsertDWORD(unsigned __int32 nVal)
{
	*(unsigned __int32*)(m_szBuf + m_nLen) = nVal;
	return (m_nLen += sizeof(unsigned __int32));
}

unsigned __int16 CPacket::InsertWORD(unsigned __int16 nVal)
{
	*(unsigned __int16*)(m_szBuf + m_nLen) = nVal;
	return (m_nLen += sizeof(unsigned __int16));
}

unsigned __int16 CPacket::InsertBYTE(unsigned __int8 nVal)
{
	*(unsigned __int8*)(m_szBuf + m_nLen) = nVal;
	return (m_nLen += sizeof(unsigned __int8));
}

unsigned __int16 CPacket::InsertString(const char* lpszString, int nLen)
{
	int		n ;

	InsertBYTE((unsigned __int8)nLen);

	for(n = 0; n < nLen; ++n)
		InsertBYTE(lpszString[n]);

	return m_nLen;
}


unsigned __int32 CPacket::GetDWORD(void)
{
	unsigned __int32 nRet = *(unsigned __int32*)(m_szBuf + m_nPos);
	m_nPos += sizeof(unsigned __int32);
	return nRet;
}

unsigned __int16 CPacket::GetWORD(void)
{
	unsigned __int16 nRet = *(unsigned __int16*)(m_szBuf + m_nPos);
	m_nPos += sizeof(unsigned __int16);
	return nRet;
}

unsigned __int8	CPacket::GetBYTE(void)
{
	unsigned __int8 nRet = *(unsigned __int8*)(m_szBuf + m_nPos);
	m_nPos += sizeof(unsigned __int8);
	return nRet;
}

unsigned __int8 CPacket::GetString(char* lpszString)
{
	unsigned __int8 nLen = GetBYTE()	,
					n					;
	for(n = 0; n < nLen; ++n)
		lpszString[n] = GetBYTE();

	return nLen;
}

void CPacket::Encode(	unsigned __int16 nStart	, 
						unsigned __int16 nLen	)
{
	unsigned __int16 n	;

	for(n = nStart; n < (nStart+nLen); ++n)
		m_szBuf[n] = ((unsigned __int8)(m_szBuf[n]) ^ 0xAD)	;
}


CPacket::~CPacket()
{ }
